package java14;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Ex2 {

	public static void main(String[] args) {
		displayAll();
	}
	public static int insert(com.mysql.jdbc.Statement stu){
		String insert = ("insert into student(stuno,name,age,birthdate) values('2015130','Jane',21,'1996-05-04');");
		String URL = "jdbc:mysql://localhost:3306/students";
		String driverName = "com.mysql.jdbc.Driver";
		String sql = "select * from student";
		String userName = "root";
		String password = "kfc123456";
		Connection conn = null;
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(URL,userName,password);
			Statement statement = conn.createStatement();
			ResultSet resultSet = statement.executeQuery(sql);
			while(resultSet.next()){
				String stuno = resultSet.getString("stuno");
				String name = resultSet.getString("name");
				Date date = resultSet.getDate("birthdate");
				System.out.print("stuno="+stuno+" name="+name+" birthdate="+date);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			conn = null;
		}
		return 0;
	}
	 public static void displayAll(){
		String display = ("select stuno,name,birthdate from student");
		String URL = "jdbc:mysql://localhost:3306/students";
		String driverName = "com.mysql.jdbc.Driver";
		String sql = "select * from student";
		String userName = "root";
		String password = "kfc123456";
		Connection conn = null;
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(URL,userName,password);
			Statement statement = conn.createStatement();
			ResultSet resultSet = statement.executeQuery(sql);
			while(resultSet.next()){
				String stuno = resultSet.getString("stuno");
				String name = resultSet.getString("name");
				Date date = resultSet.getDate("birthdate");
				System.out.print("stuno="+stuno+" name="+name+" birthdate="+date);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			conn = null;
		}
	 }
}
